/* Name: main.c
 * Author: <insert your name here>
 * Copyright: <insert your copyright message here>
 * License: <insert your license reference here>
 */

#include <avr/io.h>
#include <avr/sleep.h>
#include "avr_compiler.h"
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "TC_driver.h"
#include "clock.h"
#include "ebi.h"
#include "uart.h"
#include "twim.h"
#include "ina220.h"
#include "dma.h"

int main(void)
{
    /* insert your hardware initialization here */
    clkInit();
    
    TaskHandle_t TaskTemp;
    
    /* Set UART to 8N1 */
    uartSet8N1(&EXTUART0);
    uartSetBaud(&EXTUART0, B4800);
    uartInit(&EXTUART0);
    uartSet8N1(&EXTUART1);
    uartSetBaud(&EXTUART1, B4800);
    uartInit(&EXTUART1);
    uartSet8N1(&EXTUART2);
    uartSetBaud(&EXTUART2, B4800);
    uartInit(&EXTUART2);
    uartSet8N1(&EXTUART3);
    uartSetBaud(&EXTUART3, B4800);
    uartInit(&EXTUART3);
    uartSet8N1(&EXTUART4);
    uartSetBaud(&EXTUART4, B4800);
    uartInit(&EXTUART4);
    uartSet8N1(&EXTUART5);
    uartSetBaud(&EXTUART5, B4800);
    uartInit(&EXTUART5);
    uartSet8N1(&EXTUART6);
    uartSetBaud(&EXTUART6, B4800);
    uartInit(&EXTUART6);
    uartSet8N1(&EXTUART7);
    uartSetBaud(&EXTUART7, B4800);
    uartInit(&EXTUART7);
    
    // Initialize the TWI Master
    twi_master_init(&TWID, TWI_BAUD(32000000, 400000));
    
    // initialize the INA220
    ina220_init(INA220a_ADDR);

    // Start the DMA Handler
    xTaskCreate(dmaTask, "DMASched", 512, NULL, tskIDLE_PRIORITY, &TaskTemp);
    
    // Start the scheduler
    vTaskStartScheduler();
    
    for(;;){
        /* insert your main loop code here */
    };
    return 0;   /* never reached */
};

/* Prototype */
void vApplicationIdleHook( void );
/* This function is only called when there are no tasks to execute, except for the idle task. Most commonly it
 * is used to schedule co-routines or do some unimportant jobs. It is also great to put sleep(); to save
 * power. Microcontroller will stop code execution until the next interrupt from tick timer or peripheral.
 * configUSE_IDLE_HOOK should be defined as 1 to use IdleHook
 */
void vApplicationIdleHook( void )
{
    /* Go to sleep mode if there are no active tasks     */
    
    sleep_mode();
}
